﻿//请假
function fnSubmitVacation(isNew, isSubmit) {
    //数据传输不允许传送html的尖括号
    if (!Ext.getCmp('VacationType').validate()) {
        Ext.ux.Toast.msg('提示', '请选择请假类型！');
        return;
    }
    if (!Ext.getCmp('DaysCnt').validate()) {
        Ext.ux.Toast.msg('提示', '请输入整数或带一位小数,格式如:100，100.5');
        return;
    }
    if (Ext.getCmp('VacationType').getValue() == "调休" && Ext.getCmp('ReplacedDay').getValue()=='') {
        Ext.ux.Toast.msg('提示', '请输入对应的调休时间');
        return;
    }
    var main = {};
    //main.EmpId = Ext.getCmp('EmpId').getValue();//在服务器端读取当前用户工号
    main.VacationType = Ext.getCmp('VacationType').getValue();
    main.DaysCnt = Ext.getCmp('DaysCnt').getValue();
    main.ReplacedDay = Ext.getCmp('ReplacedDay').getValue();
    main.Cause = Ext.getCmp('Cause').getValue();
    main.StatusId = isSubmit;

    var subs = [];
    var grid = Ext.getCmp('VacationDetail');
    grid.stopEditing();
    var store = grid.getStore();
    var isSubValid = true;
    store.each(function (rs) {
        if (rs.get('PreStartTime') == '' || rs.get('PreEndTime') == '') {
            isSubValid = false;
            return;
        }
        if (rs.get('isNew') == '1') {
            subs.push({ "PreStartTime": rs.get('PreStartTime'), "PreEndTime": rs.get('PreEndTime'), "Remarks": rs.get('Remarks')});
        }
        else if (rs.dirty) {
            subs.push({ "Id": rs.get('Id'), "PreStartTime": rs.get('PreStartTime'), "PreEndTime": rs.get('PreEndTime'), "Remarks": rs.get('Remarks')});
        }
    });
    if (isNew && subs.length==0) {
        Ext.ux.Toast.msg('提示', '请增加请假细项！');
        return;
    }
    if (!isSubValid) {
        Ext.ux.Toast.msg('提示', '请输入请假开始/结束日期！');
        return;
    }
    main.ReqVacationSubs = subs;
    var records = main;
    var myMask = new Ext.LoadMask(Ext.getBody(), {
        msg: '正在保存，请稍后......',
        removeMask: true //完成后移除
    });
    myMask.show();
    var url = '/Vacation.mvc/Add';
    if (!isNew) {
        url = '/Vacation.mvc/Update';
        main.FormNo = Ext.getCmp('FormNo').getValue();
    }
    Ext.Ajax.request({
        url: url,
        method: 'POST',
        success: function (response, options) {
            myMask.hide();
            var responseMessage = Ext.util.JSON.decode(response.responseText);
            if (responseMessage.Result) {
                var formNo = Ext.getCmp('FormNo').getValue();
                if (formNo == '') { formNo = 'Form_Vaca'; }
                RefreshList(formNo);

                var win = Ext.getCmp('VacationWin');
                if (Ext.isEmpty(win)) { win = Ext.getCmp('GeneralWin'); }
                if (!Ext.isEmpty(win)) { win.close(); }

                Ext.ux.Toast.msg('提示', '数据保存成功！');
            }
            else {
                Ext.ux.Toast.msg('提示', '数据保存失败，请您重试！');
            }
        },
        params: {
            json: Ext.util.JSON.encode(records)
        },
        failure: function (form, action) {
            if (action.failureType == 'server') {
                obj = Ext.util.JSON.decode(action.response.responseText);
                Ext.Msg.alert('错误:', obj.errors.reason);
            }
            if (action.failureType == 'client') {
                Ext.ux.Toast.msg('提示', '数据不正确，请核对后重新操作');
            }
        }
    })
}

//签核函数调用
function sign(signId,fNo,evtObj) {
    var result = evtObj.value == "通过" ? 1 : 0;
    var comment = Ext.get(evtObj).parent("tr").prev().prev().dom.innerText.substr(2);
    Ext.Ajax.request({
        url: '/Sign.mvc/FormSign',
        method: 'POST',
        success: function (response, options) {
            var responseMessage = Ext.util.JSON.decode(response.responseText);
            if (responseMessage.Result) {
                Ext.MessageBox.alert("消息", responseMessage.Message);
                var win = Ext.getCmp('GeneralWin');
                if (!Ext.isEmpty(win)) { win.close(); }
                if (Ext.getCmp("daiBanGrid") != null) { Ext.getCmp("daiBanGrid").getStore().reload(); }
                //签核成功后刷新列表
                RefreshList(fNo);
            }
            else {
                Ext.MessageBox.alert("消息", responseMessage.Message);
            }
        },
        params: {
            id: signId, comment: comment, isPass: result
        },
        failure: function (form, action) {
            if (action.failureType == 'server') {
                obj = Ext.util.JSON.decode(action.response.responseText);
                Ext.Msg.alert('错误:', obj.errors.reason);
            }
            if (action.failureType == 'client') {
                Ext.ux.Toast.msg('提示', '数据不正确，请核对后重新操作');
            }
        }
    });
}

//退签
function SignnoPass(formno) {
    Ext.Ajax.request({
        url: '/Sign.mvc/noPass',
        method: 'POST',
        params: {
             formno: formno
        },
        success: function (response, options) {
            var responseMessage = Ext.util.JSON.decode(response.responseText);
            if (responseMessage.Result) {
                Ext.MessageBox.alert("消息", responseMessage.Message);
                var win = Ext.getCmp('GeneralWin');
                if (!Ext.isEmpty(win)) { win.close(); }
                if (Ext.getCmp("VacationMgr") != null) { Ext.getCmp("VacationMgr").getStore().reload(); }
                //签核成功后刷新列表
                RefreshList(formno);
            }
            else {
                Ext.MessageBox.alert("消息", responseMessage.Message);
            }
        },
        failure: function (form, action) {
            if (action.failureType == 'server') {
                obj = Ext.util.JSON.decode(action.response.responseText);
                Ext.Msg.alert('错误:', obj.errors.reason);
            }
            if (action.failureType == 'client') {
                Ext.ux.Toast.msg('提示', '数据不正确，请核对后重新操作');
            }
        }
    });
}